<template>
	<view class="user-auth-app">
		<view class="img-container">
			<image class="logo" :src="wxConfig.accountInfo.icon" />
			<view class="title">{{ wxConfig.accountInfo.nickname }}</view>
		</view>

		<view class="tip-h1">申请获取以下权限</view>
		<!--		<view v-if="!userInfo" class="tip-text">获取您的公开信息(用户头像，昵称)</view>-->
		<view class="tip-text">为了更好为您提供服务，请授权手机</view>
		<!--
		<view class="submit-btn" @click="auth">授权登录</view>
		-->
		<!--
		<button open-type="getUserInfo" @getuserinfo="bindGetUserInfo" class="submit-btn" hover-class="hover">授权登录</button>
		-->
		<!--		<button v-if="!userInfo" @click="getUserProfile" class="submit-btn" hover-class="hover">授权登录</button>-->

		<button open-type="getPhoneNumber" class="submit-btn" hover-class="hover" @getphonenumber="getPhoneNumber">
			授权登录
		</button>
		<!-- <view class="login-by-phone" @click="loginByPhone">手机验证码登录</view> -->
	</view>
</template>

<script>
import util from '@/libs/util.js';

export default {
	data() {
		return {
			wxConfig: __wxConfig,
			inviteCode: '',
			userInfo: ''
		};
	},
	onLoad(params) {
		//console.log(params);
		this.inviteCode = params.scene ? params.scene : '';
	},
	onReady() {
		//global.loginByCode()
		wx.login({
			success: (res) => {
				console.log('onReady');
				if (res.code) {
					global.setWxCode(res.code);
				}
			}
		});
	},
	onSow() {
		console.log('auth on show');
		//global.loginByCode()
		wx.login({
			success: (res) => {
				//console.log()
				if (res.code) {
					global.setWxCode(res.code);
				}
			}
		});
	},
	methods: {
		...util,
		loginByPhone() {
			this.goPage('/pages/user/auth/phoneAuth');
		},
		auth() {
			this.$Message.success('登录成功');
		},
		getUserProfile() {
			let page = this;
			wx.getUserProfile({
				desc: '用于完善会员资料',
				success: (res) => {
					console.log(res);
					this.$Message.error('为了更好为您提供服务，请授权手机');
					page.userInfo = res.userInfo;
				},
				fail: (res) => {
					this.$Message.error('获取用户信息失败');
				}
			});
		},
		bindGetUserInfo(e) {
			if (e.detail.userInfo) {
				this.submit(e);
			} else {
				this.$Message.error('用户信息不存在');
			}
		},
		getPhoneNumber(e) {
			//console.log(e);return false;
			var wxToken = uni.getStorageSync('wx_token');
			uni.showLoading({
				title: '登录中'
			});

			this.$api
				.post('/user/wxLogin', {
					code: global.getWxCode(),
					//rawData: e.detail.rawData,
					//signature: e.detail.signature,
					encryptedData: e.detail.encryptedData,
					iv: e.detail.iv,
					inviteCode: this.inviteCode,
					avatarUrl: this.userInfo.avatarUrl,
					nickName: '微信用户'
					//userInfo: e.detail.userInfo
				})
				.then((response) => {
					if (response.ret === 1) {
						global.setToken(response.data.token);
						//this.$store.commit('setGlobalToken', response.data.token)
						//this.$emit('setGlobalUserInfo', response.data)
						//this.$store.commit('setGlobalUserInfo', response.data)
						this.$Message.success('登录成功');
						uni.removeStorageSync('isJump');
						if (this.inviteCode) {
							console.log('what');
							uni.switchTab({
								url: '/pages/tea/v2/map',
								success: function (e) {
									var page = getCurrentPages().pop();
									if (page == undefined || page == null) return;
									page.onLoad();
								}
							});
						} else {
							console.log('hello');
							uni.navigateBack({
								delta: 1
							});
						}
						/*
						uni.navigateBack({
							delta: 1
						});
						*/
					} else {
						this.$Message.error(response.msg);
					}
					uni.hideLoading();
				});
		},
		submit(e) {
			var wxToken = uni.getStorageSync('wx_token');
			uni.showLoading({
				title: '登录中'
			});

			this.$api
				.post('/user/wxLogin', {
					code: global.getWxCode(),
					//rawData: e.detail.rawData,
					//signature: e.detail.signature,
					encryptedData: e.detail.encryptedData,
					iv: e.detail.iv,
					inviteCode: this.inviteCode
					//userInfo: e.detail.userInfo
				})
				.then((response) => {
					if (response.ret === 1) {
						global.setToken(response.data.token);
						this.$store.commit('setGlobalToken', response.data.token);
						//this.$emit('setGlobalUserInfo', response.data)
						//this.$store.commit('setGlobalUserInfo', response.data)
						this.$Message.success('登录成功');

						if (this.inviteCode) {
							//console.log("what");
							uni.reLaunch({ url: '/pages/tea/v2/map' });
							/*
							uni.switchTab({
							    url: '/pages/tea/v2/map',
							    success: function(e) {
							        var page = getCurrentPages().pop();
							        if (page == undefined || page == null) return;
							        page.onLoad();
							    }
							});*/
						} else {
							uni.navigateBack({
								delta: 1
							});
						}
						/*
						uni.navigateBack({
							delta: 1
						});
						*/
					} else {
						this.$Message.error(response.msg);
					}
					uni.hideLoading();
				});
		}
	}
};
</script>

<style lang="scss">
.user-auth-app {
	font-size: 32upx;
	display: flex;
	flex-direction: column;
	align-items: center;
	.login-by-phone {
		color: #3385ff;
		margin-top: 30upx;
	}

	.logo {
		width: 120upx;
		height: 120upx;
		border-radius: 100%;
		border: 1px solid #efefef;
	}

	.title {
		color: #1f1f1f;
		font-size: 38upx;
		padding-top: 20upx;
		font-weight: normal;
	}

	.img-container {
		margin-top: 50upx;
	}

	.tip-h1 {
		color: #37403d;
		margin: 30upx 0;
	}
	.tip-text {
		color: #a7a7a7;
		margin-bottom: 30upx;
	}
	.submit-btn {
		background-color: $uni-theme-color;
		/*padding: 30upx 30upx;*/
		color: white;
		width: 80%;
		text-align: center;
	}
}
</style>
